<?php 
namespace Home\Model;
use Think\Model;
use Lrxin ;

class TypeModel extends Model {

	public $tree;
	public $tid; //当前 tid



	public function _initialize()
    {
        //parent::_initialize();
        $this->tree = new \Lrxin\Tree();
        
    }

    public function __construct(){
    	parent::__construct();
		$this->init();

    }

    public function  init(){

    	$rs = $this->select();
        //初始化 tree
    	$rs = $this->tree->init($rs);
    }








    public function get_row($tid = null){
    	$tid = $tid ? $tid : $this->tid;

    	return $this->tree->get_row($tid);
    }

    //当前位置
    public function place($tid){
    	$tid = $tid ? $tid : $this->tid;
        $arr = $this->tree->get_pos($tid);
        $html = '<a href="/">首页</a> &gt;' ;
        foreach($arr as $v){
            $html .='<a href="'.typeurl($v['id']).'">'.$v['typename'].'</a> &gt;' ;
        }
        return $html;
    }

    //分页数据 list+page
    public function  listpage($pagesize , $tid=null   ){

        //默认值处理
        $tid = $tid ? $tid : $this->tid;
        $pagesize = $pagesize ? intval($pagesize) : 10 ;//每页显示记录数

        $return = array();

        //if(!$tid){return false;}

        //页码
        $p        = isset($_GET['p']) ? $_GET['p'] : 1;

        $map = array();
        if ($tid) {
            $ids = $this->tree-> ids( $tid );
            $map['a.tid'] = array( 'in' , $ids );
        };
        
        //$pagesize = 15; //每页显示记录数
        $return['list'] = $this->alias('b')
            ->field('a.id, a.tid, a.title , a.pubtime ,a.display, a.sort  , a.sortup, a.sorttime , b.typename ')
            ->join('__ARTICLE__ a ON a.tid= b.id','right')
            ->where($map)
            ->order('a.sort asc, a.sorttime desc, a.id desc')
            ->page($p, $pagesize)
            ->select();

        $count = $this->alias('b')
            ->join('__ARTICLE__ a ON a.tid= b.id','left')
            ->where($map)
            ->order('a.sort asc, a.sorttime desc, a.id desc')
            ->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $pagesize); // 实例化分页类 传入总记录数和每页显示的记录数
        //分页样式定制----setConfig
        $Page->lastSuffix = false;

        $Page->setConfig('header', '<span class="rows">共 %TOTAL_ROW% 条记录</span>');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('last', '尾页');
        //$Page->setConfig('theme', ' %HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $Page->setConfig('theme', ' %HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $return['page'] = $Page->show(); // 分页显示输出 



        //过滤 分页 url /Home/Type/index/tid/3/p/2.html  => 
        $return['page'] = preg_replace ('/\/Home\/Type\/index\/tid\/(\d+)\/p\/(\d+)/i',"/type_$1_$2", $return['page'] );




        return $return;



    }

    //修改默认排序
    public function order($s=null){
        if(!$s){
            $s = 'sort asc  , id desc ' ;
        }
        return parent::order($s);

    }



}
